package com.jie.other.network

import android.os.Bundle
import android.view.View
import android.widget.ProgressBar
import androidx.activity.result.contract.ActivityResultContracts
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.UriUtils
import com.jie.commonlib.base.BaseActivity
import com.jie.other.R

/**
 * 网络请求测试页面
 * @author jie
 */
class NetWorkActivity : BaseActivity() {
    /**
     * 文件上传进度条
     */
    private var pbUpload: ProgressBar? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.other_activity_network)
        initActionBar()
        initView()
    }

    /**
     * 初始化ActionBar
     */
    private fun initActionBar() {
        val supportActionBar = supportActionBar
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar.setTitle(R.string.other_network)
    }

    /**
     * 初始化组件
     */
    private fun initView() {
        val rvFunction = findViewById<RecyclerView>(R.id.rvFunction)
        pbUpload = findViewById(R.id.pbUpload)
        rvFunction.layoutManager = GridLayoutManager(this, 2)
        rvFunction.adapter = NetWorkAdapter(initList()).apply {
            setOnItemClickListener { _, _, position ->
                when (position) {
                    0 -> HttpTestUtils.getHttp(mHttpTag)
                    1 -> HttpTestUtils.postHttp(mHttpTag)
                    2 -> HttpTestUtils.getList(mHttpTag)
                    3 -> HttpTestUtils.getPageInfo(mHttpTag)
                    4 -> HttpTestUtils.postPageInfo(mHttpTag)
                    5 -> HttpTestUtils.postNewBuilding(mHttpTag)
                    6 -> chooseFile()
                    7 -> download()
                    8 -> cancelHttp()
                    else -> {}
                }
            }
        }
    }

    private fun initList(): ArrayList<String> {
        val homeFunctionBeans = ArrayList<String>()
        homeFunctionBeans.add("GET")
        homeFunctionBeans.add("POST")
        homeFunctionBeans.add("GET LIST")
        homeFunctionBeans.add("GET PageInfo")
        homeFunctionBeans.add("POST PageInfo")
        homeFunctionBeans.add("POST New Building")
        homeFunctionBeans.add("POST File")
        homeFunctionBeans.add("DOWNLOAD File")
        homeFunctionBeans.add("CANCEL HTTP")
        return homeFunctionBeans
    }

    private fun download() {
        showProgressDialog(0)
        HttpTestUtils.download(mHttpTag) { current, total ->
            LogUtils.i(String.format("当前下载：%s 总数：%s", current, total))
            val progress = getProgress(current, total)
            showProgressDialog(progress, 100)
        }
    }

    private fun getProgress(current: Long, total: Long): Int {
        return (current * 1.0f / total * 100).toInt()
    }

    private fun showProgressDialog(progress: Int, max: Int = 100) {
        DownloadNotificationUtils.showNotify(progress, max)
    }

    private fun showProgressView(progress: Int, max: Int = 100) {
        pbUpload!!.visibility = View.VISIBLE
        pbUpload!!.max = max
        pbUpload!!.progress = progress
    }

    private fun hideProgressView() {
        pbUpload!!.visibility = View.GONE
    }

    private val registerForActivityResult =
        registerForActivityResult(ActivityResultContracts.GetContent()) {
            val file = UriUtils.uri2File(it)
            showProgressView(0)
            HttpTestUtils.uploadFile(file, mHttpTag) { current, total ->
                LogUtils.i(String.format("当前上传：%s 总数：%s", current, total))
                if (current < total) {
                    val progress = getProgress(current, total)
                    showProgressView(progress, 100)
                } else {
                    hideProgressView()
                }
            }
        }

    private fun chooseFile() {
        registerForActivityResult.launch("*/*")
    }

    /**
     * 取消网络请求
     */
    private fun cancelHttp() {
        DownloadNotificationUtils.cancel()
    }
}